<?php
/**
 * 分页管理
 */
namespace core\helper;
class Paginate{
    
    /**
     * 后台分页 [ layui 分页适用]
     * @param $total 总条数
     * @param $page 第几页
     * @param $perpage 每页条数
     * @param $url  链接地址
     * @param $maxpage 最大页码
     * @return string 最多页数
    */
    static public function page_admin($total, $page = 1, $perpage = 20, $url = '', $maxpage = null) {
        $totalcount = $total;
        if (empty($url) || !is_string($url)) {
            $url = array();
            foreach ($_GET as $k => $v) {
                if ($k != 'page') {
                    $url[] = urlencode($k) . '=' . urlencode($v);
                }
            }
            $url[] = 'page={page}';
            $url = '?' . implode('&', $url);
        }
        if ($total <= $perpage)
            return '';
        $total = ceil($total / $perpage);
        $pagecount = $total;
        $total = ($maxpage && $total > $maxpage) ? $maxpage : $total;
        $page = intval($page);
        if ($page < 1 || $page > $total)
            $page = 1;
            if($page!=1){
                $pages = '<div class="layui-box layui-laypage layui-laypage-default"><span class="layui-laypage-count">共 '.$totalcount.' 条</span><a href="'.str_replace('{page}', 1, $url).'" class="layui-laypage-prev " data-page="1">首页</a><a href="'.str_replace('{page}', $page - 1 <= 0 ? 1 : $page - 1, $url).'" class="layui-laypage-prev" data-page="'.($page - 1 <= 0 ? 1 : $page - 1).'">上一页</a>';
            }else{
                $pages = '<div class="layui-box layui-laypage layui-laypage-default"><span class="layui-laypage-count">共 '.$totalcount.' 条</span>';
                $pages .= '<a href="javascript:;" class="layui-laypage-prev layui-disabled" data-page="1">首页</a><a href="javascript:;" class="layui-laypage-prev layui-disabled" data-page="'.($page - 1 <= 0 ? 1 : $page - 1).'">上一页</a>';
            }
        if ($page > 4 && $page <= $total - 4) {
            $mini = $page - 3;
            $maxi = $page + 2;
        } elseif ($page <= 4) {
            $mini = 2;
            $maxi = $total - 2 < 7 ? $total - 2 : 7;
        } elseif ($page > $total - 4) {
            $mini = $total - 7 < 3 ? 2 : $total - 7;
            $maxi = $total - 2;
        }
        for ($i = 1; $i <= $total; $i++) {
            if ($i != $page) {
               $pages .= '<a href="'.str_replace('{page}', $i, $url).'" data-page="'.$i.'">'.$i.'</a>';
            } else {
                $pages .= '<span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>'.$i.'</em></span>';
            }
            if ($maxi && $i >= $maxi) {
                $i = $total - 2;
                $maxi = 0;
            }
            if (($i == 2 or $total - 2 == $i) && $total > 10) {
                $pages .= '';
            }
            if ($mini && $i >= 2) {
                $i = $mini;
                $mini = 0;
            }
        }
        $pages .= '<a href="'.str_replace('{page}', $page + 1 >= $total ? $total : $page + 1, $url).'" class="layui-laypage-next" data-page="'.($page + 1 >= $total ? $total : $page + 1).'">下一页</a><span class="layui-laypage-skip">到第<input type="text" min="1" value="'.$page.'" class="layui-input" id="pageno" onkeydown="if(event.keyCode==13 &amp;&amp; this.value) {window.location.href=\'' . $url . '\'.replace(/\{page\}/, this.value);return false;}">/'.$total.'页<button type="button" class="layui-laypage-btn" onclick="if(document.getElementById(\'pageno\').value>0)window.location.href=\'' . $url . '\'.replace(/\{page\}/, document.getElementById(\'pageno\').value);">确定</button></span></div></div>';

        return $pages;
    }
}